package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.ConfigService;
import com.cskaoyan.market.db.bo.MarkerConfigWxBo;
import com.cskaoyan.market.db.vo.MallConfigVo;
import com.cskaoyan.market.db.vo.MarkerConfigOrderVo;
import com.cskaoyan.market.db.vo.MarketExpressVo;
import com.cskaoyan.market.util.GetReqBodyUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


@RestController
@RequestMapping("/admin/config")
public class AdminConfigController {
    @Autowired
    ConfigService configService;

    //private MarketExpressVo marketExpressVo=new MarketExpressVo();
    //private MarkerConfigOrderVo markerConfigOrderVo=new MarkerConfigOrderVo();
    //private MarkerConfigWxBo markerConfigWxBo=new MarkerConfigWxBo();

    @GetMapping("/wx")
    private void wxGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarkerConfigWxBo markerConfigWxBo=new MarkerConfigWxBo();
        HttpSession session = req.getSession();
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requestBody = byteArrayOutputStream.toString("utf-8");
        if (markerConfigWxBo.getMarket_wx_catlog_goods()==null){

            markerConfigWxBo.setMarket_wx_catlog_goods(6);
            markerConfigWxBo.setMarket_wx_catlog_list(4);
            markerConfigWxBo.setMarket_wx_index_brand(4);
            markerConfigWxBo.setMarket_wx_index_hot(3);
            markerConfigWxBo.setMarket_wx_index_new(3);
            markerConfigWxBo.setMarket_wx_index_topic(3);
        }
        session.setAttribute("markerConfigWxBo",markerConfigWxBo);
        Object markerConfigWxBoTemp = session.getAttribute("markerConfigWxBo");
        Object ok = ResponseUtil.ok(markerConfigWxBoTemp);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @GetMapping("/order")
    private void orderGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarkerConfigOrderVo markerConfigOrderVo=new MarkerConfigOrderVo();
        HttpSession session = req.getSession();
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
//        String requestBody = byteArrayOutputStream.toString("utf-8");
//      String freightMin = JacksonUtil.parseString(requestBody,);
        if (markerConfigOrderVo.getMarket_order_comment() == null) {
            markerConfigOrderVo.setMarket_order_comment(10);
            markerConfigOrderVo.setMarket_order_unconfirm(21);
            markerConfigOrderVo.setMarket_order_unpaid(14);
            session.setAttribute("markerConfigOrderVo",markerConfigOrderVo);
        }
        Object markerConfigOrderVoTemp = session.getAttribute("markerConfigOrderVo");
        Object ok = ResponseUtil.ok(markerConfigOrderVoTemp);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }


    @PostMapping("/wx")
    private void wxPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketExpressVo marketExpressVo=new MarketExpressVo();
        MarkerConfigOrderVo markerConfigOrderVo=new MarkerConfigOrderVo();
        MarkerConfigWxBo markerConfigWxBo=new MarkerConfigWxBo();
        HttpSession session = req.getSession();
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requestBody = byteArrayOutputStream.toString("utf-8");
        Integer marketWxCatlogGoods =   JacksonUtil.parseInteger(requestBody, "market_wx_catlog_goods");
        Integer marketWxCatlogList=   JacksonUtil.parseInteger(requestBody, "market_wx_catlog_list");
        Integer marketWxIndexBrand=   JacksonUtil.parseInteger(requestBody, "market_wx_index_brand");
        Integer marketWxIndexHot =   JacksonUtil.parseInteger(requestBody, "market_wx_index_hot");
        Integer marketWxIndexNew =   JacksonUtil.parseInteger(requestBody, "market_wx_index_new");
        Integer marketWxIndexTopic =   JacksonUtil.parseInteger(requestBody, "market_wx_index_topic");
        Boolean markerWxShare =   JacksonUtil.parseBoolean(requestBody, "market_wx_share =true");
        /**
         *     Integer market_wx_catlog_goods;
         *     Integer market_wx_catlog_list;
         *     Integer market_wx_index_brand;
         *     Integer market_wx_index_hot;
         *     Integer market_wx_index_new;
         *     Integer market_wx_index_topic;
         *     Boolean market_wx_share =true;
         */
        markerConfigWxBo.setMarket_wx_catlog_goods(marketWxCatlogGoods);
        markerConfigWxBo.setMarket_wx_catlog_list(marketWxCatlogList);
        markerConfigWxBo.setMarket_wx_index_brand(marketWxIndexBrand);
        markerConfigWxBo.setMarket_wx_index_hot(marketWxIndexHot);
        markerConfigWxBo.setMarket_wx_index_new(marketWxIndexNew);
        markerConfigWxBo.setMarket_wx_index_topic(marketWxIndexTopic);
        markerConfigWxBo.setMarket_wx_share(markerWxShare);
        session.setAttribute("markerConfigWxBo",markerConfigWxBo);
        Object markerConfigWxBoTemp = session.getAttribute("markerConfigWxBo");
        Object ok = ResponseUtil.ok(markerConfigWxBoTemp);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    @PostMapping("/order")
    private void orderPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketExpressVo marketExpressVo=new MarketExpressVo();
        MarkerConfigOrderVo markerConfigOrderVo=new MarkerConfigOrderVo();
        MarkerConfigWxBo markerConfigWxBo=new MarkerConfigWxBo();
        HttpSession session = req.getSession();
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requestBody = byteArrayOutputStream.toString("utf-8");
        Integer marketOrderComment =   JacksonUtil.parseInteger(requestBody, "Market_order_comment");
        Integer marketOrderUnconfirm = JacksonUtil.parseInteger(requestBody, "Market_order_unconfirm");
        Integer marketOrderUnpaid = JacksonUtil.parseInteger(requestBody, "Market_order_unpaid");
        markerConfigOrderVo.setMarket_order_comment(marketOrderComment);
        markerConfigOrderVo.setMarket_order_unconfirm(marketOrderUnconfirm);
        markerConfigOrderVo.setMarket_order_unpaid(marketOrderUnpaid);
        session.setAttribute("markerConfigOrderVo",markerConfigOrderVo);
        Object ok = ResponseUtil.ok(markerConfigOrderVo);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @PostMapping("/express")
    private void expressPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketExpressVo marketExpressVo=new MarketExpressVo();

        HttpSession session = req.getSession();
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requestBody = byteArrayOutputStream.toString("utf-8");
        String freightMin = JacksonUtil.parseString(requestBody, "market_express_freight_min");
        String freightValue = JacksonUtil.parseString(requestBody, "market_express_freight_value");
        marketExpressVo.setMarket_express_freight_min(freightMin);
        marketExpressVo.setMarket_express_freight_value(freightValue);
        session.setAttribute("marketExpressVo",marketExpressVo);
        Object ok = ResponseUtil.ok(marketExpressVo);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @GetMapping("/express")
    private void expressGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketExpressVo marketExpressVo=new MarketExpressVo();
        MarkerConfigOrderVo markerConfigOrderVo=new MarkerConfigOrderVo();
        MarkerConfigWxBo markerConfigWxBo=new MarkerConfigWxBo();
        HttpSession session = req.getSession();
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requestBody = byteArrayOutputStream.toString("utf-8");
        if (marketExpressVo.getMarket_express_freight_min()==null){
            marketExpressVo.setMarket_express_freight_min("12");
            marketExpressVo.setMarket_express_freight_value("5");
        }
        

//        String freightMin = String.valueOf(session.getAttribute("market_express_freight_min"));
//        String freightValue = String.valueOf(session.getAttribute("market_express_freight_value"));
//        String freightMin = JacksonUtil.parseString(requestBody, "market_express_freight_min");
        session.setAttribute("marketExpressVo",marketExpressVo);
        Object marketExpressVoTemp = session.getAttribute("marketExpressVo");
        Object ok = ResponseUtil.ok(marketExpressVoTemp);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @PostMapping("/mall")
    private void updateMall(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetReqBodyUtil.getRequestBody(req);
        MallConfigVo mallConfigVo = JacksonUtil.getObjectMapper().readValue(requestBody, MallConfigVo.class);
        int code = configService.updateMall(mallConfigVo);
        if (code == 0) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
        }else {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.fail()));
        }
    }

    @GetMapping("/mall")
    private void queryMall(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MallConfigVo mallConfigVo = new MallConfigVo();
        mallConfigVo = configService.queryMall();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(mallConfigVo)));
    }
}


